import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {nanoid} from 'nanoid';
import './index.css';

export default class Header extends Component {

  // 添加
  addListItem = (e) => {
    if (e.keyCode !== 13) return
    const value = e.target.value
    if (value.trim() === '') return alert('输入框不能为空！')
    const listObj = {
      id: nanoid(),
      text: e.target.value,
      done: false
    }
    this.props.addList(listObj)
    e.target.value = ''
  }

  // 设置 prop 的数据类型
  static propTypes = {
    addList: PropTypes.func.isRequired
  }

  render() {
    return (
      <div className="todo-header">
        <input onKeyUp={this.addListItem} type="text" placeholder="请输入你的任务名称，按回车键确认" />
      </div>
    );
  }
}
